﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Adams.Public.Api.Controllers
{
    [EnsureHttps]
    public class ClassifiedsController : Controller
    {
        public ActionResult GetTypesAndCategories()
        {
            var model = DataAccess.Classifieds.GetTypesAndCategories();
            return new JsonXmlResult(model);
        }

        public ActionResult ListPostings(Adams.Public.DataTransfer.Classifieds.ListPostingsInput input)
        {
            var model = DataAccess.Classifieds.ListPostings(input);
            return new JsonXmlResult(model);
        }

        public ActionResult SearchPostings(Adams.Public.DataTransfer.Classifieds.SearchPostingsInput input)
        {
            var model = DataAccess.Classifieds.SearchPostings(input);
            return new JsonXmlResult(model);
        }

        public ActionResult Posting(Adams.Public.DataTransfer.Classifieds.PostingInput input)
        {
            var model = DataAccess.Classifieds.Posting(input);
            return new JsonXmlResult(model);
        }

        public ActionResult GetMemberPostings(Adams.Public.DataTransfer.Classifieds.GetMemberPostingsInput input)
        {
            input.MemberID = Api.Security.GetMemberIDFromToken(input.Token);
            var model = DataAccess.Classifieds.GetMemberPostings(input);
            return new JsonXmlResult(model);
        }

        public ActionResult Renew(Adams.Public.DataTransfer.Classifieds.RenewInput input)
        {
            input.MemberID = Api.Security.GetMemberIDFromToken(input.Token);
            var output = new Adams.Public.DataTransfer.FormOutput();

            var save = new Adams.Public.DataTransfer.Classifieds.RenewSave();
            Shared.ObjectHelper.CopyObject(output, ref save);

            save.ExpiresOn = DateTime.Now.AddDays(14);

            DataAccess.Classifieds.Renew(save);

            output.IsSaved = true;

            return new JsonXmlResult(output);
        }

        public ActionResult Remove(Adams.Public.DataTransfer.Classifieds.RemoveInput input)
        {
            input.MemberID = Api.Security.GetMemberIDFromToken(input.Token);
            var output = new Adams.Public.DataTransfer.FormOutput();

            DataAccess.Classifieds.Remove(input);

            output.IsSaved = true;

            return new JsonXmlResult(output);
        }
    }
}